<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="../../pub/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>属性设置</title>
<%@include file="../../pub/resource.jsp"%>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/select2.css" />
<script type="text/javascript" src="${pageContext.request.contextPath }/js/select2.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/select2_locale_zh-CN.js"></script>
<style type="text/css">
body {
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}

@media ( max-width : 980px) {
	/* Enable use of floated navbar text */
	.navbar-text.pull-right {
		float: none;
		padding-left: 5px;
		padding-right: 5px;
	}
}
</style>
</head>
<body>
	<div class="definewidth m20">
		<shiro:hasPermission name="属性设置:添加">
		<button type="button" class="btn btn-success" id="addnew">新增设置</button></shiro:hasPermission>
		&nbsp;&nbsp;
		<shiro:hasPermission name="属性设置:删除">
		<input type="button" class="btn btn-danger" id="delSet"
			value="删除设置" /></shiro:hasPermission>
	</div>
	<table class="table table-bordered table-hover definewidth m10">
		<thead>
			<tr>
				<th style="width: 60px;"><input type="checkbox" id="cBoxAll"
					name="cBoxAll" /></th>
				<th>序号</th>
				<th>父属性</th>
				<th>属性</th>
				<th>值</th>
				<th>描述</th>
				<th>操作</th>
			</tr>
		</thead>
		<c:forEach begin="0" step="1" items="${list }" var="gl"
			varStatus="var">
			<tr>
				<td><input type="checkbox" name="cBoxItem" value="${gl.id }" /></td>
				<td>${var.count}</td>
				<td>${gl.showParentSet }</td>
				<td>${gl.key }</td>
				<td>${gl.value }</td>
				<td>${gl.descript }</td>
				<td>
				<shiro:hasPermission name="属性设置:编辑">
				<a href="javascript:void"
					onclick="editPosition(${gl.id},'${gl.parentKey }','${gl.key }','${gl.value }','${gl.descript }')">编辑</a>
					</shiro:hasPermission>
			</tr>
		</c:forEach>
		<c:if test="${empty list }">
			<tr>
				<td colspan="7">暂无数据</td>
			</tr>
		</c:if>
	</table>
</body>
<script type="text/javascript">
var parentSet=${parentSet};
	$(function() {
		$("#addnew").click(
						function() {
							currentWin.$.dialog({
										title : "增加属性",
										lock : true,
										background : 'gray', // 背景色
										opacity : 0.65, // 透明度
										content : '<div><table><tr><td algin="right">父属性：</td><td><input type="hidden" id="parentKey" name="parentKey"/></td></tr>'
										+'<tr><td algin="right">属性名：</td><td><input type="text" name="key" id="key" placeholder="请输入属性名"/></td></tr>'
										+'<tr><td algin="right">属性值：</td><td><input type="text" name="value" id="value" placeholder="请输入属性值"/></td></tr>'+
										'<tr><td algin="right">描述：</td><td><textarea id="descript" name="descript" cols="40" rows="3"></textarea></td></tr></table></div>',
										ok : function() {
											var key = currentWin.$("#key").val();
											var parentKey = currentWin.$("#parentKey").val();
											if (key == "") {
												alertWin("请输入要添加的属性名","warning");
												return false;
											}
											var value=currentWin.$("#value").val();
											if($.trim(value)==""){
												alertWin("请输入要添加的属性值","warning");
												return false;
											}
											var descript=currentWin.$("#descript").val();
											$.ajax({
														url : "add.do",
														type : "post",
														dataType : "text",
														data : {
															parentKey:parentKey,
															key : key,
															value:value,
															descript:descript
														},
														success : function(data) {
															if (data == "success") {
																alertWinWithRefresh("添加成功！","succeed");
															}else{
																alertWin("添加失败！","error");
															}
														}
													});
											return true;
										},
										okVal : "提交"
									});
							currentWin.$("#parentKey").select2({
								width:"150",
								data:parentSet,
								placeHolder:"请选择父属性"
							});
							currentWin.$("#parentKey").select2("val",0);
						});
		$("#cBoxAll").click(
				function() {
					$("[name='cBoxItem']:checkbox")
							.attr(
									"checked",
									$(this).attr("checked") == "checked" ? true
											: false);
				});
		$("#delSet").click(
				function() {
					if(confirm("你确定要进行删除操作？")){
						var str = "";
						$("[name='cBoxItem']:checkbox:checked").each(
								function(index, element) {
									str += $(this).val() + ",";
								})
						if (str != "") {
							str = str.substring(0, str.length - 1);
						}
						if (str == "") {
							alertWin("请选中要删除的项！","warning");
							return;
						}
						$.ajax({
							url : "del.do",
							type : "post",
							data : {
								ids : str
							},
							dataType : "text",
							success : function(data) {
								if (data == "success") {
									alertWinWithRefresh("删除成功！","succeed");
								}
							}
						});
					}
				});
	});
	function editPosition(id,parentKey,key,value,descript){
		currentWin.$.dialog({
			title : "修改属性",
			lock : true,
			background : 'gray', // 背景色
			opacity : 0.65, // 透明度
			content : '<div><table><tr><td algin="right">父属性：</td><td><input type="hidden" id="parentKey" name="parentKey"/></td></tr>'
			+'<tr><td algin="right">属性名：</td><td><input type="text" name="key" id="key" value="'+key+'" placeholder="请输入属性名"/></td></tr>'
				+'<tr><td algin="right">属性值：</td><td><input type="text" name="value" id="value" value="'+value+'" placeholder="请输入属性值"/></td></tr>'+
				'<tr><td algin="right">描述：</td><td><textarea id="descript" name="descript" cols="40" rows="3">'+descript+'</textarea></td></tr></table></div>',
				ok : function() {
					var parentkey=currentWin.$("#parentKey").val();
					if(trim(parentkey)==id){
						alertWin("父键不能选择自己！","warning");
						return false;
					}
					var key = currentWin.$("#key").val();
					if (key == "") {
						alertWin("请输入要添加的属性名","warning");
						return false;
					}
					var value=currentWin.$("#value").val();
					if($.trim(value)==""){
						alertWin("请输入要添加的属性值","warning");
						return false;
					}
					var descript=currentWin.$("#descript").val();
				$.ajax({
							url : "edit.do",
							type : "post",
							dataType : "text",
							data : {
								id:id,
								parentKey:parentkey,
								key : key,
								value:value,
								descript:descript
							},
							success : function(data) {
								if (data == "success") {
									alertWinWithRefresh("修改成功！","succeed");
								}else{
									alertWin("修改失败！","error");
								}
							}
						});
				return true;
			},
			okVal : "提交"
		});
		currentWin.$("#parentKey").select2({
			width:"150",
			data:parentSet,
			placeHolder:"请选择父属性"
		});
		currentWin.$("#parentKey").select2("val",parentKey);
	}
</script>
</html>